<?php

namespace app\aso\controller;

use app\aso\Auth;
use app\common\cachemodel\AsoCheckLog;
use app\common\cachemodel\AsoSensorsdataUsers;
use app\common\cachemodel\SystemAsoCompleteLog;
use app\common\cachemodel\SystemAsoStartLog;
use app\common\lib\SensorsData;

/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2022/9/26
 * Time: 1:28 PM
 */
class Sensors extends Auth
{
    /**
     * 检查用户是否安装过app
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function check()
    {
        $param['channelid'] = input('channelid/d', '');//渠道标识
        $param['idfa'] = input('idfa/s', '');//手机的idfa
        $param['os'] = input('os/s', '');//系统版本
        $param['ip'] = input('ip/s', '');//用户的ip地址
        $param['apptype'] = input('apptype/s', '');//设备型号
        $param['time'] = input('time/d', '');//设备型号
        $param['keyword'] = input('keyword/s', '');

        if ($param['channelid'] !== 105)
        {
            return show(config('code.error'), 'Invalid channelid');
        }

        $data = (new AsoSensorsdataUsers())->where(['idfa' => $param['idfa']])->count('id');

        if ($data > 0)
        {
            $isRepeat = 1;
        } else
        {
            $isRepeat = 0;
        }
        // 记录日志
        (new AsoCheckLog())->saveData($param['idfa'], $data > 0 ? 2 : 1);

        return show(config('code.success'), 'Success.', ['is_repeat' => $isRepeat]);//设备排重是否通过(0：通过；其他是不通过）
    }

    /**
     * 通知开始任务
     * @return \think\response\Json
     */
    public function click()
    {
        $param['channelid'] = input('channelid/d', '');//渠道标识
        $param['idfa'] = input('idfa/s', '');//手机的idfa
        $param['os'] = input('os/s', '');//系统版本
        $param['ip'] = input('ip/s', '');//用户的ip地址
        $param['apptype'] = input('apptype/s', '');//设备型号
        $param['time'] = input('time/d', '');//设备型号
        $param['keyword'] = input('keyword/s', '');
        if ($param['channelid'] !== 105)
        {
            return show(config('code.error'), 'Invalid channelid');
        }
        //日志
        (new SystemAsoStartLog())->saveData($param);

        //  通知开始任务    ASOTaskStart    打点
        SensorsData::track($param['idfa'], false, 'ASOTaskStart', [
            'aso_idfa' => $param['idfa'],
            'aso_channelid' => $param['channelid'],
            'aso_os' => $param['os'],
            'aso_ip' => $param['ip'],
            'aso_apptype' => $param['apptype'],
            'aso_time' => $param['time'],
        ]);

        return show(config('code.success'), 'Success.');
    }


    /**
     * 通知任务完成
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function appSubmit()
    {
        $param['channelid'] = input('channelid/d', '');//渠道标识
        $param['idfa'] = input('idfa/s', '');//手机的idfa
        $param['os'] = input('os/s', '');//系统版本
        $param['ip'] = input('ip/s', '');//用户的ip地址
        $param['apptype'] = input('apptype/s', '');//设备型号
        $param['time'] = input('time/d', '');//设备型号
        $param['keyword'] = input('keyword/s', '');

        if ($param['channelid'] !== 105)
        {
            return show(config('code.error'), 'Invalid channelid');
        }

        $startLog = (new SystemAsoStartLog())->where('idfa', $param['idfa'])->count('id');
        if ($startLog)
        {
            //日志
            (new SystemAsoCompleteLog())->saveData($param);

            $data = (new AsoSensorsdataUsers())->where(['idfa' => $param['idfa']])->count('id');
            if ($data < 1)
            {
                //入库
                (new AsoSensorsdataUsers())->saveData($param['idfa']);
            }

            //  通知任务完成    ASOTaskComplete    打点
            SensorsData::track($param['idfa'], false, 'ASOTaskComplete', [
                'aso_idfa' => $param['idfa'],
                'aso_channelid' => $param['channelid'],
                'aso_os' => $param['os'],
                'aso_ip' => $param['ip'],
                'aso_apptype' => $param['apptype'],
                'aso_time' => $param['time'],
            ]);

            return show(config('code.success'), 'Success.');
        }
        return show(config('code.error'), 'Error.');

    }
}
